package com.dongdong.controller;


import com.alibaba.fastjson.JSONObject;

import com.dongdong.api.OrderApi;
import com.dongdong.api.PayApi;
import com.dongdong.result.Result;

import com.dongdong.utils.JwtUtil;
import com.dongdong.vo.AddressVO;
import com.dongdong.vo.OrderVO;
import com.dongdong.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("toOrders")
public class OrderController {

    @Autowired
    private OrderApi orderApi;
    @Autowired
    private PayApi payApi;
    @Autowired
    private JwtUtil jwtUtil;


    @GetMapping("/selectAllByUsername")
    public Result selectAllByUsername(@RequestHeader String token){

        //解析token
        Claims claims = jwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        UserVO userVO = JSONObject.parseObject(subject, UserVO.class);


        List<OrderVO> orderVOS = orderApi.selectAllByUsername(userVO.getUsername());
        return Result.OK().setData(orderVOS);

    }

    //退款
    @GetMapping("refundQuery")
    public Result refundQuery(String tradeNo,String refundAmount,String outRequestNo,String outTradeNo){

        String str =  payApi.refundQuery(tradeNo,refundAmount,outRequestNo);
        orderApi.UpdateRefundQuery(outTradeNo);
        return Result.OK().setData(str);
    }

}
